/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.bensoft.minesweeper.gui;

import com.bensoft.minesweeper.ApplicationController;
import com.bensoft.minesweeper.gui.preferences.ApplicationPreferences;
import com.bensoft.minesweeper.resources.strings.StringResources;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;

/**
 *
 * @author Ben Wolsieffer
 */
public class HighScoreDialog extends JDialog implements GUILoader {

    JPanel scorePanel, buttonPanel;
    JLabel beginnerScoreLabel, beginnerNameLabel,
            intermediateScoreLabel, intermediateNameLabel,
            advancedScoreLabel, advancedNameLabel;
    JButton resetButton, okButton;

    public HighScoreDialog() {
        super(ApplicationController.getMinesweeperFrame());
        init();
    }

    private void init() {
        initializeComponents();
        setLayouts();
        addComponents();
        addComponentListeners();
        otherInit();
    }

    public void initializeComponents() {
        beginnerScoreLabel = new JLabel(Integer.toString(ApplicationPreferences.getBeginnerHighScore()) + " seconds");
        beginnerNameLabel = new JLabel(ApplicationPreferences.getBeginnerHighScoreName());
        intermediateScoreLabel = new JLabel(Integer.toString(ApplicationPreferences.getIntermediateHighScore()) + " seconds");
        intermediateNameLabel = new JLabel(ApplicationPreferences.getIntermediateHighScoreName());
        advancedScoreLabel = new JLabel(Integer.toString(ApplicationPreferences.getAdvancedHighScore()) + " seconds");
        advancedNameLabel = new JLabel(ApplicationPreferences.getAdvancedHighScoreName());
        scorePanel = new JPanel();
        resetButton = new JButton(StringResources.getString("gui.dialog.highscore.button.reset"));//Reset Scores
        okButton = new JButton(StringResources.getString("gui.button.ok"));
        buttonPanel = new JPanel();
    }

    public void setLayouts() {
        setLayout(new BorderLayout());
        scorePanel.setLayout(new GridLayout(0, 3, 12, 3));
        buttonPanel.setLayout(new BorderLayout());
    }

    public void addComponents() {
        scorePanel.add(new JLabel(StringResources.getString("gui.dialog.highscore.label.beginner") + ": "));
        scorePanel.add(beginnerScoreLabel);
        scorePanel.add(beginnerNameLabel);
        scorePanel.add(new JLabel(StringResources.getString("gui.dialog.highscore.label.intermediate") + ": "));
        scorePanel.add(intermediateScoreLabel);
        scorePanel.add(intermediateNameLabel);
        scorePanel.add(new JLabel(StringResources.getString("gui.dialog.highscore.label.advanced") + ": "));
        scorePanel.add(advancedScoreLabel);
        scorePanel.add(advancedNameLabel);
        add(scorePanel, BorderLayout.NORTH);
        buttonPanel.add(resetButton, BorderLayout.WEST);
        buttonPanel.add(okButton, BorderLayout.EAST);
        add(buttonPanel, BorderLayout.SOUTH);
    }

    public void addComponentListeners() {
        resetButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                beginnerScoreLabel.setText("999 " + StringResources.getString("gui.dialog.highscore.label.seconds"));
                beginnerNameLabel.setText(StringResources.getString("gui.dialog.highscore.label.defaultname"));
                ApplicationPreferences.setBeginnerHighScore(999);
                ApplicationPreferences.setBeginnerHighScoreName(StringResources.getString("gui.dialog.highscore.label.defaultname"));
                intermediateScoreLabel.setText("999 " + StringResources.getString("gui.dialog.highscore.label.seconds"));
                intermediateNameLabel.setText(StringResources.getString("gui.dialog.highscore.label.defaultname"));
                ApplicationPreferences.setIntermediateHighScore(999);
                ApplicationPreferences.setAdvancedHighScoreName(StringResources.getString("gui.dialog.highscore.label.defaultname"));
                advancedScoreLabel.setText("999 " + StringResources.getString("gui.dialog.highscore.label.seconds"));
                advancedNameLabel.setText(StringResources.getString("gui.dialog.highscore.label.defaultname"));
                ApplicationPreferences.setAdvancedHighScore(999);
                ApplicationPreferences.setAdvancedHighScoreName(StringResources.getString("gui.dialog.highscore.label.defaultname"));
            }
        });
        okButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                dispose();
            }
        });
    }

    public void otherInit() {
        okButton.requestFocusInWindow();
        setTitle(StringResources.getString("gui.dialog.highscore.title"));
        setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
        scorePanel.setBorder(BorderFactory.createEmptyBorder(20, 10, 15, 10));
        buttonPanel.setBorder(BorderFactory.createEmptyBorder(0, 35, 10, 35));
        setResizable(false);
        setModal(true);
        pack();
    }

    @Override
    public void setVisible(boolean b) {
        beginnerScoreLabel.setText(Integer.toString(ApplicationPreferences.getBeginnerHighScore()) + " " + StringResources.getString("gui.dialog.highscore.label.seconds"));
        beginnerNameLabel.setText(ApplicationPreferences.getBeginnerHighScoreName());
        intermediateScoreLabel.setText(Integer.toString(ApplicationPreferences.getIntermediateHighScore()) + " " + StringResources.getString("gui.dialog.highscore.label.seconds"));
        intermediateNameLabel.setText(ApplicationPreferences.getIntermediateHighScoreName());
        advancedScoreLabel.setText(Integer.toString(ApplicationPreferences.getAdvancedHighScore()) + " " + StringResources.getString("gui.dialog.highscore.label.seconds"));
        advancedNameLabel.setText(ApplicationPreferences.getAdvancedHighScoreName());
        setLocationRelativeTo(ApplicationController.getMinesweeperFrame());
        super.setVisible(b);
    }
}
